

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.logging &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.logging</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.logging 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">CRITICAL</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">DEBUG</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">ERROR</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">FATAL</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">INFO</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">WARN</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">WARNING</span>  <span class="c1"># NOQA</span>
<span class="kn">import</span> <span class="nn">threading</span>

<span class="kn">import</span> <span class="nn">colorlog</span>

<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>  <span class="c1"># NOQA</span>

<span class="n">_lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
<span class="n">_default_handler</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># type: Optional[logging.Handler]</span>


<span class="k">def</span> <span class="nf">create_default_formatter</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">colorlog</span><span class="o">.</span><span class="n">ColoredFormatter</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Create a default formatter of log messages.</span>

<span class="sd">    This function is not supposed to be directly accessed by library users.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">return</span> <span class="n">colorlog</span><span class="o">.</span><span class="n">ColoredFormatter</span><span class="p">(</span>
        <span class="s2">&quot;</span><span class="si">%(log_color)s</span><span class="s2">[</span><span class="si">%(levelname)1.1s</span><span class="s2"> </span><span class="si">%(asctime)s</span><span class="s2">]</span><span class="si">%(reset)s</span><span class="s2"> </span><span class="si">%(message)s</span><span class="s2">&quot;</span>
    <span class="p">)</span>


<span class="k">def</span> <span class="nf">_get_library_name</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>

    <span class="k">return</span> <span class="vm">__name__</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>


<span class="k">def</span> <span class="nf">_get_library_root_logger</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">logging</span><span class="o">.</span><span class="n">Logger</span><span class="p">:</span>

    <span class="k">return</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">_get_library_name</span><span class="p">())</span>


<span class="k">def</span> <span class="nf">_configure_library_root_logger</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

    <span class="k">global</span> <span class="n">_default_handler</span>

    <span class="k">with</span> <span class="n">_lock</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">_default_handler</span><span class="p">:</span>
            <span class="c1"># This library has already configured the library root logger.</span>
            <span class="k">return</span>
        <span class="n">_default_handler</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">StreamHandler</span><span class="p">()</span>  <span class="c1"># Set sys.stderr as stream.</span>
        <span class="n">_default_handler</span><span class="o">.</span><span class="n">setFormatter</span><span class="p">(</span><span class="n">create_default_formatter</span><span class="p">())</span>

        <span class="c1"># Apply our default configuration to the library root logger.</span>
        <span class="n">library_root_logger</span> <span class="o">=</span> <span class="n">_get_library_root_logger</span><span class="p">()</span>
        <span class="n">library_root_logger</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">_default_handler</span><span class="p">)</span>
        <span class="n">library_root_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
        <span class="n">library_root_logger</span><span class="o">.</span><span class="n">propagate</span> <span class="o">=</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">_reset_library_root_logger</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>

    <span class="k">global</span> <span class="n">_default_handler</span>

    <span class="k">with</span> <span class="n">_lock</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">_default_handler</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">library_root_logger</span> <span class="o">=</span> <span class="n">_get_library_root_logger</span><span class="p">()</span>
        <span class="n">library_root_logger</span><span class="o">.</span><span class="n">removeHandler</span><span class="p">(</span><span class="n">_default_handler</span><span class="p">)</span>
        <span class="n">library_root_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">NOTSET</span><span class="p">)</span>
        <span class="n">_default_handler</span> <span class="o">=</span> <span class="kc">None</span>


<span class="k">def</span> <span class="nf">get_logger</span><span class="p">(</span><span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">logging</span><span class="o">.</span><span class="n">Logger</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Return a logger with the specified name.</span>

<span class="sd">    This function is not supposed to be directly accessed by library users.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_configure_library_root_logger</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>


<div class="viewcode-block" id="get_verbosity"><a class="viewcode-back" href="../../reference/logging.html#optuna.logging.get_verbosity">[文档]</a><span class="k">def</span> <span class="nf">get_verbosity</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Return the current level for the Optuna&#39;s root logger.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Logging level, e.g., ``optuna.logging.DEBUG`` and ``optuna.logging.INFO``.</span>

<span class="sd">    .. note::</span>
<span class="sd">        Optuna has following logging levels:</span>

<span class="sd">        - ``optuna.logging.CRITICAL``, ``optuna.logging.FATAL``</span>
<span class="sd">        - ``optuna.logging.ERROR``</span>
<span class="sd">        - ``optuna.logging.WARNING``, ``optuna.logging.WARN``</span>
<span class="sd">        - ``optuna.logging.INFO``</span>
<span class="sd">        - ``optuna.logging.DEBUG``</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_configure_library_root_logger</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">_get_library_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">getEffectiveLevel</span><span class="p">()</span></div>


<div class="viewcode-block" id="set_verbosity"><a class="viewcode-back" href="../../reference/logging.html#optuna.logging.set_verbosity">[文档]</a><span class="k">def</span> <span class="nf">set_verbosity</span><span class="p">(</span><span class="n">verbosity</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Set the level for the Optuna&#39;s root logger.</span>

<span class="sd">    Args:</span>
<span class="sd">        verbosity:</span>
<span class="sd">            Logging level, e.g., ``optuna.logging.DEBUG`` and ``optuna.logging.INFO``.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_configure_library_root_logger</span><span class="p">()</span>
    <span class="n">_get_library_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">verbosity</span><span class="p">)</span></div>


<div class="viewcode-block" id="disable_default_handler"><a class="viewcode-back" href="../../reference/logging.html#optuna.logging.disable_default_handler">[文档]</a><span class="k">def</span> <span class="nf">disable_default_handler</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Disable the default handler of the Optuna&#39;s root logger.</span>

<span class="sd">    Example:</span>

<span class="sd">        Stop and then resume logging to :obj:`sys.stderr`.</span>

<span class="sd">        .. testsetup::</span>

<span class="sd">            def objective(trial):</span>
<span class="sd">                x = trial.suggest_uniform(&#39;x&#39;, -100, 100)</span>
<span class="sd">                y = trial.suggest_categorical(&#39;y&#39;, [-1, 0, 1])</span>
<span class="sd">                return x ** 2 + y</span>

<span class="sd">        .. testcode::</span>

<span class="sd">            import optuna</span>

<span class="sd">            study = optuna.create_study()</span>

<span class="sd">            # There are no logs in sys.stderr.</span>
<span class="sd">            optuna.logging.disable_default_handler()</span>
<span class="sd">            study.optimize(objective, n_trials=10)</span>

<span class="sd">            # There are logs in sys.stderr.</span>
<span class="sd">            optuna.logging.enable_default_handler()</span>
<span class="sd">            study.optimize(objective, n_trials=10)</span>
<span class="sd">            # [I 2020-02-23 17:00:54,314] Trial 10 finished with value: ...</span>
<span class="sd">            # [I 2020-02-23 17:00:54,356] Trial 11 finished with value: ...</span>
<span class="sd">            # ...</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_configure_library_root_logger</span><span class="p">()</span>

    <span class="k">assert</span> <span class="n">_default_handler</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
    <span class="n">_get_library_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">removeHandler</span><span class="p">(</span><span class="n">_default_handler</span><span class="p">)</span></div>


<div class="viewcode-block" id="enable_default_handler"><a class="viewcode-back" href="../../reference/logging.html#optuna.logging.enable_default_handler">[文档]</a><span class="k">def</span> <span class="nf">enable_default_handler</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Enable the default handler of the Optuna&#39;s root logger.</span>

<span class="sd">    Please refer to the example shown in :func:`~optuna.logging.disable_default_handler()`.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_configure_library_root_logger</span><span class="p">()</span>

    <span class="k">assert</span> <span class="n">_default_handler</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
    <span class="n">_get_library_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">_default_handler</span><span class="p">)</span></div>


<div class="viewcode-block" id="disable_propagation"><a class="viewcode-back" href="../../reference/logging.html#optuna.logging.disable_propagation">[文档]</a><span class="k">def</span> <span class="nf">disable_propagation</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Disable propagation of the library log outputs.</span>

<span class="sd">    Note that log propagation is disabled by default.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_configure_library_root_logger</span><span class="p">()</span>
    <span class="n">_get_library_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">propagate</span> <span class="o">=</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="enable_propagation"><a class="viewcode-back" href="../../reference/logging.html#optuna.logging.enable_propagation">[文档]</a><span class="k">def</span> <span class="nf">enable_propagation</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Enable propagation of the library log outputs.</span>

<span class="sd">    Please disable the Optuna&#39;s default handler to prevent double logging if the root logger has</span>
<span class="sd">    been configured.</span>

<span class="sd">    Example:</span>

<span class="sd">        Propagate all log output to the root logger in order to save them to the file.</span>

<span class="sd">        .. testsetup::</span>

<span class="sd">            def objective(trial):</span>
<span class="sd">                x = trial.suggest_uniform(&#39;x&#39;, -100, 100)</span>
<span class="sd">                y = trial.suggest_categorical(&#39;y&#39;, [-1, 0, 1])</span>
<span class="sd">                return x ** 2 + y</span>

<span class="sd">        .. testcode::</span>

<span class="sd">            import optuna</span>
<span class="sd">            import logging</span>

<span class="sd">            logger = logging.getLogger()</span>

<span class="sd">            logger.setLevel(logging.INFO)  # Setup the root logger.</span>
<span class="sd">            logger.addHandler(logging.FileHandler(&quot;foo.log&quot;, mode=&quot;w&quot;))</span>

<span class="sd">            optuna.logging.enable_propagation()  # Propagate logs to the root logger.</span>
<span class="sd">            optuna.logging.disable_default_handler()  # Stop showing logs in sys.stderr.</span>

<span class="sd">            study = optuna.create_study()</span>

<span class="sd">            logger.info(&quot;Start optimization.&quot;)</span>
<span class="sd">            study.optimize(objective, n_trials=10)</span>

<span class="sd">            with open(&#39;foo.log&#39;) as f:</span>
<span class="sd">                assert f.readline() == &quot;Start optimization.\\n&quot;</span>
<span class="sd">                assert f.readline().startswith(&quot;Trial 0 finished with value:&quot;)</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_configure_library_root_logger</span><span class="p">()</span>
    <span class="n">_get_library_root_logger</span><span class="p">()</span><span class="o">.</span><span class="n">propagate</span> <span class="o">=</span> <span class="kc">True</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>